package view.GUI.adminView.coreData.people.person;
import model.data.tableModel.person.Person;
import model.data.tableModel.person.Staff;

import org.controlsfx.validation.ValidationSupport;

import view.Validator.NameValidator;
import view.Validator.EMailValidator;
import view.Validator.IntegerNumberRangeValidator;
import enums.GUIText;
import enums.Status;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;


public final class NewPersonGrid extends GridPane{

	private Label lblFirstName;
	private Label lblLastName;
	private Label lblRoom;
	private Label lblMail;
	
	private TextField txtfirstName;
	private TextField txtLastName;
	private TextField txtRoom;
	private TextField txtMail;
	
	private ValidationSupport valSup;
	
	private Label lblShowNick;
	
	public NewPersonGrid(ValidationSupport valSup){
		this.valSup = valSup;
		createWidgets();
		addWidgets();
		setupLayout();
		setupInteraction();
		setupValidation();
	}

	public NewPersonGrid(Label lblShowNick, ValidationSupport valSup) {
		this(valSup);
		this.lblShowNick = lblShowNick;
		txtfirstName.textProperty().addListener(new ChangeListener<String>(){

			@Override
			public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
				calculateNick();
			}
		});
		
		txtLastName.textProperty().addListener(new ChangeListener<String>(){

			@Override
			public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
				calculateNick();
			}
		});
		
		txtRoom.textProperty().addListener(new ChangeListener<String>(){

			@Override
			public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
				calculateNick();
			}
		});
	}
	
	private void calculateNick(){
		String firstName = txtfirstName.getText();
		String lastName = txtLastName.getText();
		String room = txtRoom.getText();
		
		if(firstName != null && lastName != null && room != null && !"".equals(firstName) && !"".equals(lastName) && !"".equals(room)){
			String nick = Staff.createNickname(firstName, lastName, Integer.valueOf(room));			
			lblShowNick.setText(nick);
		}
		
		
	}
	public void disableForEdit(){
		txtfirstName.setDisable(true);
		txtLastName.setDisable(true);
	}

	private void setupValidation() {
		valSup.registerValidator(txtfirstName, true, new NameValidator());
		valSup.registerValidator(txtLastName, true, new NameValidator());
		valSup.registerValidator(txtRoom, true, IntegerNumberRangeValidator.createMaxNumberRangeValidator(800));
		valSup.registerValidator(txtMail, true, new EMailValidator());
	}

	private void createWidgets() {
		lblFirstName = new Label(GUIText.FIRSTNAME.toString());
		lblLastName = new Label(GUIText.LASTNAME.toString());
		lblRoom = new Label(GUIText.ROOM.toString());
		lblMail = new Label(GUIText.MAIL.toString());
		
		txtfirstName = new TextField();
		txtLastName = new TextField();
		txtRoom = new TextField();
		txtMail = new TextField();
	}

	private void addWidgets() {
		this.add(lblFirstName, 0, 0);
		this.add(lblLastName, 0, 1);
		this.add(lblRoom, 0, 2);
		this.add(lblMail, 0, 3);
		
		this.add(txtfirstName, 1, 0);
		this.add(txtLastName, 1, 1);
		this.add(txtRoom, 1, 2);
		this.add(txtMail, 1, 3);
		
	}

	private void setupLayout() {
		this.setVgap(10);
		this.setHgap(10);
	}

	private void setupInteraction() {
		// TODO Auto-generated method stub
		
	}
	
	public String getMail(){
		return txtMail.getText();
	}
	
	public String getRoom(){
		return txtRoom.getText();
	}

	/**
	 * If all information were entered in the grid this method returns a new person with the given data otherwise it returns null.
	 * @return
	 */
	public Person getPerson() {
		Person p = null;
		if(!txtfirstName.textProperty().isEmpty().getValue() || !txtLastName.textProperty().isEmpty().getValue() || !txtMail.textProperty().isEmpty().getValue() || !txtRoom.textProperty().isEmpty().getValue()){
			p = Person.createNewPerson(txtfirstName.getText().trim(), txtLastName.getText().trim(), Integer.valueOf(txtRoom.getText().trim()), Status.ACTIVE, txtMail.getText().trim());
		}
		return p;
	}

	public void setPerson(Person s) {
		this.txtfirstName.setText(s.getFirstName());
		this.txtLastName.setText(s.getLastName());
		this.txtMail.setText(s.getEmail());
		this.txtRoom.setText(String.valueOf(s.getRoom()));
	}
}
